package com.shuaiche.yeguan.views.dialog;

import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.TextView;
import android.widget.Toast;

import com.shuaiche.yeguan.R;
import com.shuaiche.yeguan.config.SCAppConfig;
import com.shuaiche.yeguan.ui.common.WebviewActivity;

import androidx.annotation.NonNull;

public class PrivacyDialog extends Dialog {
    private Context context;
    private OnPrivacyResultListener listener;
    private CheckBox checkBox;

    public PrivacyDialog(@NonNull Context context) {
        super(context, R.style.CustomDialog);
        this.context = context;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dialog_privacy);

        // 设置对话框宽度为屏幕宽度的80%
        Window window = getWindow();
        if (window != null) {
            WindowManager.LayoutParams params = window.getAttributes();
            params.width = (int) (getScreenWidth(context) * 0.8);
            window.setAttributes(params);
        }

        // 初始化视图
        TextView tvTitle = findViewById(R.id.tv_title);
        TextView tvContent = findViewById(R.id.tv_content);
        TextView tv_service = findViewById(R.id.tv_service);
        TextView tv_privacy = findViewById(R.id.tv_privacy);
        checkBox = findViewById(R.id.checkbox);
        Button btnConfirm = findViewById(R.id.btn_confirm);
        Button btnRefuse = findViewById(R.id.btn_refuse);

        tv_service.setOnClickListener(v -> {
            Intent bundle = new Intent(context, WebviewActivity.class);
            bundle.putExtra("url", SCAppConfig.URL_PROTOCOL_SERVICE);
            bundle.putExtra("bundleTitle", "用户协议");
            context.startActivity(bundle);
        });
        tv_privacy.setOnClickListener(v -> {
            Intent bundle = new Intent(context, WebviewActivity.class);
            bundle.putExtra("url", SCAppConfig.URL_PROTOCOL_PRIVACY);
            bundle.putExtra("bundleTitle", "隐私协议");
            context.startActivity(bundle);
        });

        // 设置点击事件
        btnConfirm.setOnClickListener(v -> {
            if (!checkBox.isChecked()) {
                Toast.makeText(context, "请阅读并同意用户协议和隐私政策", Toast.LENGTH_SHORT).show();
                return;
            }
            if (listener != null) {
                listener.onConfirm();
            }
            dismiss();
        });

        btnRefuse.setOnClickListener(v -> {
            if (listener != null) {
                listener.onRefuse();
            }
            dismiss();
        });
    }

    private int getScreenWidth(Context context) {
        WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
        DisplayMetrics metrics = new DisplayMetrics();
        wm.getDefaultDisplay().getMetrics(metrics);
        return metrics.widthPixels;
    }

    public void setOnPrivacyResultListener(OnPrivacyResultListener listener) {
        this.listener = listener;
    }

    public interface OnPrivacyResultListener {
        void onConfirm();
        void onRefuse();
    }
}